<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<button id="btn" type="button">点我发送请求</button>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script src="index.js"></script>
<script type="text/javascript">
    $("#btn").click(function(){
        // 创建WebSocket 对象
        var ws = new WebSocket("wss://tx-sh-live-comet-02.chat.bilibili.com/sub");
        //连接成功时，触发事件
        ws.onopen = function () {
            //请求参数
            //var param = {"id": 1,"command": "account_info","account": "r9cZA1mLK5R5Am25ArfXFmqgNwjZgnfk59"};
            var param = {"roomid":21569100};
            // 使用 send() 方法发送数据
            ws.send(JSON.stringify(param));
            alert("数据发送中...");
        }
        //接收到服务端响应的数据时，触发事件
        ws.onmessage = function (evt) {
            var data = evt.data;
            alert("收到数据..."+data);
        }
        // 断开 web socket 连接成功触发事件
        ws.onclose = function () {
            alert("连接已关闭...");
        };
    });
</script>
</html>